<?php

namespace App\Console\Commands;

use App\Jobs\TestJob;
use App\Models\Article;
use App\Models\Category;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;

class MyTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:my-test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // 同步执行
//        $result = Process::run('pwd');
//        $this->info($result->output());
//          // 同步执行并输出日志
//        $result = Process::path(base_path())->run('php artisan app:test-job');
//        $this->info($result->output());
//          // 同步执行并实时输出日期
//        Process::path(base_path())->run('php artisan app:test-job', function (string $type, string $output) {
//            echo $output;
//        });
        // 异步执行
        Process::path(base_path())->run('php artisan app:test-job &');
        return;

        dispatch(new TestJob());
        return;

        \DB::listen(function($query) {
            $bindings = $query->bindings;
            $sql = $query->sql;
            foreach ($bindings as $replace){
                $value = is_numeric($replace) ? $replace : "'".$replace."'";
                $sql = preg_replace('/\?/', $value, $sql, 1);
            }
            \Log::info('sql语句：' . $sql);
        });

//        $article = Article::query()
//            ->with('categoryTest')
//            ->orderByDesc('id')->first();
//        $this->info(json_encode($article, JSON_UNESCAPED_UNICODE));

        $category = Category::query()
            ->with('articles')
            ->find(4);
        $this->info(json_encode($category, JSON_UNESCAPED_UNICODE));
        return;

        $user = new User();
        $user->name = 'admin';
        $user->email = 'admin@163.com';
        $user->password = '123456';
        $user->save();

//        $user = User::first();
//        $user->name = 'admin001';
//        $user->save();

//        User::first()->delete();

//        $article = new Article();
//        $article->title = '天气不错';
//        $article->content = '今天多云转晴';
//        $article->save();
        $this->info('保存成功');
    }
}
